(ns liberator-sample.core.tutorial.decision-graph
  (:require [liberator.core :refer [defresource resource]]
            [compojure.core :refer [defroutes ANY]]
            [ring.middleware.params :refer [wrap-params]]
            [org.httpkit.server :as server]))

(declare app)
(defroutes app
           (ANY "/" [] "Hello Liberator!")
           (ANY "/secret" []
                (resource :available-media-types ["text/html"]
                          :exists? (fn [ctx]
                                     (prn (get-in ctx [:request]))
                                     (= "tiger" (get-in ctx [:request :params "word"])))
                          :handle-ok "You found the secret word!"
                          :handle-not-found "Uh, that's the wrong word. Guess again!"))
           (ANY "/choice" []
                (resource :available-media-types ["text/html"]
                          :exists? (fn [ctx]
                                     (if-let [choice (get {"1" "stone", "2" "paper", "3" "scissors"}
                                                          (get-in ctx [:request :params "choice"]))]
                                       {:choice choice}))
                          :handle-ok (fn [ctx]
                                       (format "<html>Your choice: '%s'.</html>"
                                               (get ctx :choice)))
                          :handle-not-found (fn [ctx]
                                              (format "<html>There is no value for the option '%s'</html>"
                                                      (get-in ctx [:request :params "choice"] "")))
                          )))

(server/run-server (wrap-params app) {:port 3333})